package com.kdtech.analyse.news;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.alibaba.fastjson.JSONObject;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class ChinaNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		String[] regex={
				"http://.*[.]china[.]com/.*/[0-9]*/[0-9]{8}/[0-9]*[.]html",
				"http://.*[.]china.com/[0-9]{6}/[0-9]*[-][a-z]{1}[0-9]*[.]html",
				"http://health[.]china[.]com/.*/[0-9]{6}/[0-9]*[-][0-9]*[.]html",
				"http://www.[a-z]+.chinanews.com/[0-9]{4}/[0-9]+/[0-9]+.html",
				"http://.*..*.chinanews.com/.*/[0-9]*/[0-9]*/[0-9]*.html"
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String html=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		String title="";
		String content="";
		Long date=null;
		String author=null;
		NewsMeta meta=new NewsMeta();
		meta.setUrl(urlMeta.getUrl());
		meta.setType(0);
		Document doc=Jsoup.parse(html);


		doc.select(".edit_related2013").remove();
		doc.select(".adclass").remove();
		doc.select(".chan_gHD_tj").remove();
		author=doc.select("div#chan_newsInfo a[target=blank]").text();
		if(author==null){
			author=doc.select(".source").text();
		}
		/*
		 * 解析新闻标题
		 */
		title=doc.select("h1#chan_newsTitle").text();
		if (StringUtils.isBlank(title)) {
			title=doc.select("div#chan_newsTitle").text();
			if (StringUtils.isBlank(title)) {
				title=doc.select("div.art_l_text>h1").text();
				if (StringUtils.isBlank(title)) {
					title=doc.select("h1.chan_gHD_tit").text();
					if (StringUtils.isBlank(title)) {
						title=doc.select("h1.articleTit").text();
						if (StringUtils.isBlank(title)) {
							title=doc.select("h1.news-article-tit").text();
						}
					}
				}

			}
		}
		if(StringUtils.isBlank(title)){
			title=doc.select(".article-content-title").text();
		}
		/*
		 * 解析时间
		 */
		date=DateUtils.matchDate(doc.select("div#chan_newsInfo").text());
		if (date == null) {
			date=DateUtils.matchDate(doc.select("div.art_artInfo").text());
			if (date == null) {
				date=DateUtils.matchDate(doc.select(".chan_gHD_info")
						.text());
				if (date == null) {
					date=DateUtils.matchDate(doc.select("div.articleAtt")
							.text());
					if (date == null) {
						date=DateUtils.matchDate(doc.select("span.time")
								.text());
					}
				}
			}

		}
		if(date==null){
			date=DateUtils.matchDate(doc.select(".date").text());
		}

		content=JSoupUtils.selectContent(url, doc,"div#chan_newsDetail p","div#artibody p","div.articleBody p");
		if(StringUtils.isBlank(content)){
			content=doc.select(".article-detail").html();
		}
		meta.setTitle(title);
		meta.setContent(content);
		meta.setAuthor(author);
		meta.setDate(date);


		/*
		 * 解析评论数的Ajax地址
		 */
		Matcher matcher=COMMENTSCRIPT.matcher(html);
		if (matcher.find()) {
			String group=matcher.group();
			String replace=group.replace("includeCommentnum('", "");
			replace=replace.substring(0, replace.length() - 2);
			meta.setUpdateUrl(getPath(replace));
			NewsMeta update=Update(meta);
			if (update != null) {
				meta.setCommentNum(update.getCommentNum());
				meta.setClickNum(update.getClickNum());
			}
		}

		return meta;
	}

	private final static Pattern COMMENTSCRIPT=Pattern
			.compile("includeCommentnum\\(\\'(.*)\\'\\)");

	
	public NewsMeta Update(NewsMeta meta) {
		try {
			if (meta != null) {
				String ajaxUrl=meta.getUpdateUrl();
				if (ajaxUrl != null) {
					String getHtml=CrawlHTML.GetHtml(ajaxUrl, "UTF-8");
					getHtml=getHtml.replace("var commentnum=", "");
					getHtml=getHtml.substring(0, getHtml.length() - 1);
					JSONObject json=JSONObject.parseObject(getHtml);
					/**
					 * 发表评论数
					 */
					int commentnum=json.getJSONObject("news").getIntValue("commentnum");
					/**
					 * 评论被顶得数量
					 */
					int topnum=json.getJSONObject("news").getIntValue("topnum");
					/**
					 * 总评论数=发表评论 + 评论被顶的数量
					 */
					meta.setCommentNum(commentnum + topnum);
					return meta;
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			// 只有在评论数为0 的时候，目标站点服务器没有对应生成JS,产生404错误
			// System.out.println("获取评论数失败-->"+meta.getUrl());
		}
		return null;
	}

	private String getPath(String newsid) {
		String path=newsid.substring(3, newsid.length());
		String syspath=newsid.substring(0, 3);

		int l=path.length();
		for (int i=8; i > l; i--) {
			path="0" + path;
		}
		String path5=path.substring(path.length() - 1, path.length());
		String path4=path.substring(path.length() - 3, path.length() - 1);
		String path3=path.substring(path.length() - 5, path.length() - 3);
		String path2=path.substring(0, path.length() - 5);
		String path1="/data/";
		return "http://club.china.com" + path1 + syspath + "/" + path2 + "/"
				+ path3 + "/" + path4 + "/" + path5 + "num.js";
	}

	public static void main(String[] args) {
		String url="http://www.jx.chinanews.com/news/2016/1108/3413.html";
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		ChinaNewsAnalyse a=new ChinaNewsAnalyse();
		boolean detailPage=a.isDetailPage(url);
		if (detailPage) {
			NewsMeta parserHtml=a.parserHtml(urlMeta);
			System.out.println(parserHtml);
		} else {
			System.out.println("不符合正则");
		}
	}

	
	public boolean isNeedUpdate(){
		return true;
	}
}
